from PySide6.QtWidgets import QWidget
from widgets.SwitchButton import SwitchButton


class ScriptWidget(QWidget):
    DEFAULT_WIDTH = 420
    DEFAULT_LINE_HEIGHT = 30
    DEFAULT_INTERVAL_X = 5
    DEFAULT_INTERVAL_Y = 5
    NEW_LINE_SPACE_WIDTH = 30

    def __init__(self, _type=None):
        super().__init__()

        self.type = _type

        # 设置默认值
        self.btnDelete = None
        self.btnInsert = None
        self.onInsert = None
        self.onDelete = None
        self._currentLine = 0
        self._new_line_space_width = ScriptWidget.NEW_LINE_SPACE_WIDTH
        self._isNewLine = False
        self._intervalX = ScriptWidget.DEFAULT_INTERVAL_X
        self._intervalY = ScriptWidget.DEFAULT_INTERVAL_Y
        self._lineHeight = ScriptWidget.DEFAULT_LINE_HEIGHT
        self.setFixedSize(ScriptWidget.DEFAULT_WIDTH, ScriptWidget.DEFAULT_LINE_HEIGHT)
        self._data = {}
        self._lastWidget = None

    def addOperate(self):
        from ..scriptWidgets.ScriptButton import ScriptButton
        self.btnDelete = ScriptButton(self, '删除', self.callDelete)
        self.addWidget(self.btnDelete)

        self.btnInsert = ScriptButton(self, '+', self.onInsert)
        self.addWidget(self.btnInsert)

        self.switchEnable = SwitchButton()
        self.switchEnable.setOnText('开')
        self.switchEnable.setOffText('关')
        self.switchEnable.setGeometry(self.switchEnable.x(), self.switchEnable.y(), 50, self.lineHeight)
        self.switchEnable.setChecked(True)
        self.addWidget(self.switchEnable)

    def setOnDelete(self, onDelete):
        if self.btnDelete is None:
            return

        self.onDelete = onDelete
        self.btnDelete.clicked.connect(self.callDelete)

    def callDelete(self):
        if self.onDelete:
            self.onDelete(self)

        self.close()
        self.destroy()
        self.deleteLater()

    def setOnInsert(self, event):
        if self.btnInsert is None:
            return

        self.onInsert = event
        self.btnInsert.clicked.connect(self.callInsert)

    def callInsert(self):
        if self.onInsert is None:
            return

        self.onInsert(self)

    @property
    def type(self):
        return self._type

    @type.setter
    def type(self, value):
        self._type = value

    @property
    def data(self):
        return self._data

    def setData(self, data):
        self._data = data
        if 'enable' in data and self.switchEnable:
            self.switchEnable.setChecked(data['enable'])

    def addWidget(self, widget: QWidget):
        widget.setParent(self)
        if self.lastWidget is None:
            widget.setGeometry(
                self.contentsMargins().left(), self.contentsMargins().top(), widget.width(),
                widget.height())
            self._lastWidget = widget
            return
        elif self._isNewLine:
            y = self.lastWidget.y() + self.lastWidget.height() + self.intervalY
            widget.setGeometry(
                self.newLineSpaceWidth,
                y,
                widget.width(),
                widget.height()
            )

            self.setFixedHeight(y + self.lineHeight)
            self._isNewLine = False
            self._lastWidget = widget
            return

        if self.lastWidget.x() + self.lastWidget.width() + self.intervalX + widget.width() > self.width():
            self.newLine(False)
            height = (self.intervalY + self.lineHeight) * self.currentLine + self.lineHeight
            self.setFixedHeight(height)
            widget.setGeometry(
                self.newLineSpaceWidth,
                self.lastWidget.y() + self.lastWidget.height() + self.intervalY,
                widget.width(),
                widget.height()
            )
            self._lastWidget = widget
            return

        widget.setGeometry(
            self.contentsMargins().left() + self.lastWidget.x() + self.lastWidget.width() + self.intervalX,
            self.lastWidget.y(),
            widget.width(),
            widget.height()
        )

        self._lastWidget = widget

    @property
    def lineHeight(self):
        return self._lineHeight

    @lineHeight.setter
    def lineHeight(self, lineHeight):
        self._lineHeight = lineHeight

    @property
    def intervalX(self):
        return self._intervalX

    @intervalX.setter
    def intervalX(self, interval):
        self._intervalX = interval

    @property
    def intervalY(self):
        return self._intervalY

    @intervalY.setter
    def intervalY(self, interval):
        self._intervalY = interval

    @property
    def currentLine(self):
        return self._currentLine

    def newLine(self, target=True):
        self._currentLine += 1
        if target:
            self._isNewLine = True

    @property
    def newLineSpaceWidth(self):
        return self._new_line_space_width

    @newLineSpaceWidth.setter
    def newLineSpaceWidth(self, width):
        self._new_line_space_width = width

    @property
    def lastWidget(self):
        return self._lastWidget

    def error(self, error):
        return {
            'error': error
        }
